package dsk.demo.product.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dsk.demo.common.utils.PageUtils;
import dsk.demo.product.dao.CustomerDao;
import dsk.demo.product.dto.CustomerQueryDto;
import dsk.demo.product.entity.CustomerEntity;
import dsk.demo.product.entity.ProductEntity;
import dsk.demo.product.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {
    @Autowired
    CustomerDao customerDao;

    @Override
    public PageUtils queryPage(CustomerQueryDto dto) {
        //分页对象
        Page<CustomerEntity> page = new Page<>(dto.getPage(), dto.getLimit());
        IPage<CustomerEntity> productListByPage = customerDao.getCustomerListByPage(page,dto);
        PageUtils pageUtils = new PageUtils(productListByPage);
        return pageUtils;
    }

    @Override
    public List<CustomerEntity> excel2CustomerEntityList(MultipartFile file) throws Exception {
        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(inputStream);
            Map<String, String> map = new HashMap<>();
            map.put("国家","country");
            map.put("公司名称","companyName");
            map.put("公司ID","id");
            map.put("海关编号","hsListStr");
            reader.setHeaderAlias(map);
            List<CustomerEntity> all = reader.readAll(CustomerEntity.class);
            return all;
        }catch (Exception e) {
            System.out.println("Excel 解析失败");
            throw new Exception("Excel 解析失败");
        }
    }

    @Override
    public void insertBatch(List<CustomerEntity> customerEntities) {
        customerDao.insertBatch(customerEntities);
    }
}
